package main;

import java.util.*;

public class Wiki {
    public List<Article> articles;
    public Map<String, Article> synonymsMap;
    //private int numberOfArtikles;

    public Wiki(List<Article> articles) {
        //this.numberOfArtikles = numberOfArtikles;
        this.articles = articles;
        synonymsMap = createSynonsmsMap();
    }

    public Map<String, Article> createSynonsmsMap() {
        Map<String, Article> symonymsMap = new HashMap<String, Article>();
        for(Article article: articles) {
            symonymsMap.put(article.name, article);
            for(int i = 0; i < article.numberOfSynonyms; i ++) {
                symonymsMap.put(article.synonyms.get(i), article);
            }
        }
        return symonymsMap;
    }

    public boolean isSynonym(String word, Article article){
        String temp = "";
        if(!Character.isLetter(word.charAt(word.length() - 1))) {
            temp = "" + word.charAt(0);
            temp = temp.toUpperCase();
            temp += word.substring(1, word.length() - 1);
            word = temp;
        }
        else {
            temp = "" + word.charAt(0);
            temp = temp.toUpperCase();
            temp += word.substring(1, word.length());
            word = temp;
        }
        word.trim();

        if(synonymsMap.containsKey(word)) {
            Article tmp = synonymsMap.get(word);
            if(tmp.equals(article)) {
                return true;
            }
        }
        return false;
    }

    public String createLink(String word, Article article) {
        String link = "";
        if(!Character.isLetter(word.charAt(word.length() - 1))) {
            StringBuilder sb = new StringBuilder(word);
            word = word.substring(0, word.length() - 1);
            if(word.equalsIgnoreCase(article.name)) {
                link = "[[" + word + "]]" + sb.charAt(sb.length() - 1);
            }
            else {
                link = "[[" + article.name + "|" + word + "]]" + sb.charAt(sb.length() - 1);
            }
        }
        else {
            if(word.equalsIgnoreCase(article.name)) {
                link = "[[" + word + "]]";
            }
            else {
                link = "[[" + article.name + "|" + word + "]]";
            }
        }
        return link;
    }

    public void linkageArticles(Article a1, Article a2) {
        //в а2 проставляются ссылки на а1
        // List<List<String - одна строка со словами>> - сипсок списков всех строк слов
        List<List<String>> allWords = a2.getWords();
        List<List<String>> newWords = new ArrayList<List<String>>();
        for(int i = 0; i < allWords.size(); i++) {
            ArrayList<String> temp = new ArrayList<String>(allWords.get(i));
            List<String> tmp = new ArrayList<String>();
            for(int j = 0; j < temp.size(); j++) {
                if(isSynonym(temp.get(j), a1)) {
                    temp.set(j, createLink(temp.get(j), a1));
                }
                tmp.add(temp.get(j));
            }
            newWords.add(tmp);
        }
        a2.updateArticle(newWords);
    }

    public void linkageAllArticles() {
        for(int i = 0; i < articles.size() - 1; i++) {
            for (int j = 1; j < articles.size(); j++) {
                if(i != j) {
                    linkageArticles(articles.get(i), articles.get(j));
                    linkageArticles(articles.get(j), articles.get(i));
                }
            }
        }
    }


}
